/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

// -----------------------------------------------------------------------------
// Contains a simple method for showing and hiding information, in a drop-down
// like fashion.
//
// Usage:
//
// -----------------------------------------------------------------------------

// Some configuration parameters
var DROPDOWN_DELAY = 1;        // Animation delay
var DROPDOWN_DELTA = 1;        // Number of pixels to increment on each frame

// Some globals
var g_currentSection = null;   // The section we are currently animating
var g_currentParent  = null;   // Parent of the section we are hiding



//
// Toggles the visibility of the specified component.
// @param a_parentID    ID of the parent container
// @param a_componentID ID of the component to hide or show
// @param a_bAnimate    If true, the show/hide action is animated
//
function toggleSectionByID( a_parentID, a_componentID, a_bAnimate )
{
    // Get the component
    var component = document.getElementById( a_componentID );
    var parent    = document.getElementById( a_parentID    );
    
    if( component )
    {
        // Detect current status
        var isExpanded = component.style.display != "none";
        if (isExpanded) {            
            hideSection( parent, component, a_bAnimate );
        } else {
            showSection( parent, component, a_bAnimate );
        }        
    }
        
}

//
// Hides the specified element
// @param a_parentID  ID of the parent container
// @param a_component Document component to hide
// @param a_bAnimate  If true, animate this operation
//
function hideSectionByID( a_parentID, a_componentID, a_bAnimate )
{
    // Get the component
    var component = document.getElementById( a_componentID );
    var parent    = document.getElementById( a_parentID    );
    
    if( component ) {
        hideSection( parent, component, a_bAnimate );
    }    
}

//
// Shows the specified element
// @param a_parentID    ID of the parent container
// @param a_componentID ID of the component to show
// @param a_bAnimate    If true, animate this operation
//
function showSectionByID( a_parentID, a_componentID, a_bAnimate )
{
    // Get the component
    var component = document.getElementById( a_componentID );
    var parent    = document.getElementById( a_parentID    );
    
    if( component ) {
        showSection(parent,component, a_bAnimate);
    }    
    
}

//
// Shows the specified element
// @param a_parent    The parent container
// @param a_component The component to show
// @param a_bAnimate  If true, animate this operation
//
function showSection( a_parent, a_component, a_bAnimate )
{
    if( a_component )
    {
        if( a_bAnimate == false ) { 
            a_component.style.display = "block";
        }
        else
        {
            if( g_currentSection == null )
            {
                g_currentSection          = a_component;
                a_component.style.display = "block";
                a_component.style.height  = DROPDOWN_DELTA + "px";
                var finalHeight           = a_component.scrollHeight;
                
                // Hide the component until it is completely expanded
                for( var i = 0; i < a_component.childNodes.length; i++ )
                {
                    a_component.childNodes[i].visibility = "hidden";
                }
                
                // Schedule next increment
                setTimeout( "stepDropdownAnimation(" + DROPDOWN_DELTA + "," + finalHeight + "," + DROPDOWN_DELTA + ")", DROPDOWN_DELAY );
            }
        }
        
    }
}

//
// Hides the specified element
// @param a_parent    The parent container
// @param a_component The component to hide
// @param a_bAnimate  If true, animate this operation
//
function hideSection( a_parent, a_component, a_bAnimate )
{
    if( a_component )
    {
        if (a_bAnimate == false ) {
            a_component.style.display = "none";
        }
        else
        {
            if( g_currentSection == null )
            {
                var containerSize        = a_parent.scrollHeight
                var sectionSize          = a_component.scrollHeight
                
                g_currentSection         = a_component;
                g_currentParent          = a_parent;
                
                g_currentSection.style.display = "none";
                var newHeight                  = containerSize - DROPDOWN_DELTA
                g_currentParent.style.height   = Math.max(0, newHeight)  + "px";
                var dropdownStep               = -DROPDOWN_DELTA;
                var targetHeight               = containerSize - sectionSize;
                
                                
                // Schedule next increment
                setTimeout( "stepDropdownAnimation(" + newHeight + "," + targetHeight + "," + dropdownStep + ")", DROPDOWN_DELAY );
            }
            
        }
    }
}

//
// Animation function to hide/show a section
function stepDropdownAnimation( a_currentHeight, a_finalHeight, a_step )
{
    if( g_currentSection )
    {        
        // Next step
        var newHeight = a_currentHeight + a_step;
        
        //alert('new = current('+g_currentSection.style.height+') + step('+a_step+') = ' + newHeight);
        // We might have finished closing
        if ( Math.abs(a_currentHeight - a_finalHeight) <= Math.abs(a_step) )
        {
            g_currentParent.style.height = a_finalHeight + "px"; 
            g_currentSection = null;                
            
            if( a_step > 0 )
            {
                g_currentSection.style.display = "block";
            }
            else
            {
                // ...
            }
        }
        else
        {
            // Apply, and schedule next stepping
            g_currentParent.style.height = newHeight + "px";
            setTimeout( "stepDropdownAnimation(" + newHeight + "," + a_finalHeight + "," + a_step + ")", DROPDOWN_DELAY );
        }
    }
}


